package il.ac.braude.sona.control;
import il.ac.braude.sona.constants.ButtonPermission;
import il.ac.braude.sona.constants.Mssg;
import il.ac.braude.sona.constants.P;
import il.ac.braude.sona.constants.Resources;
import il.ac.braude.sona.entities.ChangeRequest;
import il.ac.braude.sona.gui.forms.FormFrame;
import il.ac.braude.sona.gui.forms.FormListener;
import il.ac.braude.sona.gui.forms.myforms.ReportTestFailursForm;
import il.ac.braude.sona.gui.infogui.RequsetInfoFrame;
import il.ac.braude.sona.ocsf.client.Client;
import il.ac.braude.sona.ocsf.client.ServerResponseListener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JOptionPane;

/**
 * This class is in charge of controlling tester's actions. 
 * @author  Ofek Ron
 * @author Avihay Avitan
 * @author Nitzan Inbar
 * @author Sasha Rozenthal
 * Laboratory in Software Engineering,Ort Braude College.</p>
 *
 */
public class TesterController implements FormListener{
	private ReportTestFailursForm reportFailursForm;
	private ReportTestFailursForm reportFailursInfo;
	private RequsetInfoFrame  reqFram;
	ChangeRequest myReq;

	
	@Override
	public void onFormCanceled(FormFrame formFrame) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void onFormSubmited(final FormFrame formFrame, ArrayList<Object> values) {
		if (formFrame==reportFailursForm){
		if (values.contains("")){
				reportFailursForm.showMessege("MUST FILL DESCRIPTION!");
				values.clear();
			}
			else{
			try {
				Client c = Client.getInstance();
				values.add(myReq.getID());
				Mssg mssg = new Mssg(ButtonPermission.ReportTestFailures,values);
				c.sendToServer(new ServerResponseListener() {
					@Override
					public void onServerResponse(Mssg mssg) {
						if (mssg.is(P.error)) {
							reportFailursForm.showMessege((String) mssg.getMessege());
							return;
						}
						JOptionPane.showMessageDialog(reportFailursForm,Resources.Strings.TestReportSaved);
						formFrame.dispose();
						formFrame.dispose();
						reqFram.dispose();
					}
				},mssg);
			 
			} catch (Exception e) {
				reportFailursForm.showMessege(Resources.Strings.ConnectFailed);
			}
			}
		}
		else if (formFrame==reportFailursInfo){
			formFrame.dispose();
		}

	}
	
	
/**
 * 
 * This method is in charge of controlling the tester's reported failures.
 * @param req Change request to check.
 * @param requsetInfoFrame request GUI
 * 
 */
	
	public void CheckForFailursReport(ChangeRequest req,RequsetInfoFrame requsetInfoFrame) {
		reqFram=requsetInfoFrame;
		myReq=req;
		try {
			Client c = Client.getInstance();
			Mssg mssg= new Mssg(P.checkForFailurs,req.getID());
			c.sendToServer(new ServerResponseListener() {
				@Override
				public void onServerResponse(Mssg mssg) {
					if (mssg.is(P.error)) {
						JOptionPane.showMessageDialog(null,"cant get failures from DB");
						return;
					}
					
					else if (mssg.getMessege()!=null){
						@SuppressWarnings("unchecked")
						final ArrayList<String> information=new ArrayList<String>((ArrayList<String>)mssg.getMessege());
						reqFram.addButton(ButtonPermission.ViewReportFailurs,new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e) {
							reportFailursInfo=new ReportTestFailursForm (information);
							reportFailursInfo.addFormListener(TesterController.this);
							reportFailursInfo.showFrame();	
						 }
					});
						reqFram.pack();
					}
				}
			},mssg);
		 
		} catch (Exception e) {
			JOptionPane.showMessageDialog(reqFram,"cant get failures from DB");
		}
		
	}
/**
 * This method is in charge of reporting tester-detected failures and save them to database. 
 * @param req Change request to check.
 * @param requsetInfoFrame request GUI
 */
	public void execReportTestFailurs(ChangeRequest req,RequsetInfoFrame requsetInfoFrame) {
		myReq=req;
		reqFram=requsetInfoFrame;
		reportFailursForm=new ReportTestFailursForm(req);
		reportFailursForm.addFormListener(TesterController.this);
		reportFailursForm.showFrame();		
	}

	public void approveTestModificated(ChangeRequest req, RequsetInfoFrame requsetInfoFrame) {
		reqFram=requsetInfoFrame;
		myReq=req;
		try {
			Client c = Client.getInstance();
			Mssg mssg= new Mssg(ButtonPermission.ModificationTested,req.getID());
			c.sendToServer(new ServerResponseListener() {
				@Override
				public void onServerResponse(Mssg mssg) {
					if (mssg.is(P.error)) {
						JOptionPane.showMessageDialog(null,"cant update testing DB");
						return;
					}
					else {
						JOptionPane.showMessageDialog(reqFram,"Tester modifications approved!");
						reqFram.dispose();
						reqFram.dispose();
					}
				}
			},mssg);
		 
		} catch (Exception e) {
			JOptionPane.showMessageDialog(reqFram,"cant UPDATE DB");
		}
		
	}




}
